// 获取收货地址
// 绑定点击事件
// 调用小程序的内置api   获取用户的收货地址   wx.chooseAddress
// 把收货地址存储到本地存储当中




import { showModal, showToast } from '../../utils/asyncWx.js'


Page({

    // 点击收货地址触发的事件
    handleChooseAddress() {
        // 获取收货地址
        wx.chooseAddress({
            success: (result) => {
                // console.log(result);
                // 存入本地存储
                result.all = result.provinceName + result.cityName + result.countyName + result.detailInfo
                wx.setStorageSync('address', result);
            },
        });
    },


    /**
     * 页面的初始数据
     */
    data: {
        address: {},
        cart: [],
        allChecked: false,
        totalPrice: 0,
        totalNum: 0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    onShow() {
        const address = wx.getStorageSync("address");

        // 获取缓存中的购物车数据
        const cart = wx.getStorageSync("cart") || [];

        // const allChecked = cart.length ?  cart.every((item)=>item.checked) : false
        this.setData({
            address
        })
        this.setCart(cart)
    },



    // 商品选中
    handleItemChange(e) {
        const goods_id = e.currentTarget.dataset.id

        let cart = this.data.cart
        let index = cart.findIndex((item) => item.goods_id === goods_id)
        cart[index].checked = !cart[index].checked

        this.setCart(cart)

    },


    // 设置购物车状态 还有价格 数量  总价格   总数量
    setCart(cart) {
        // 计算全选
        let allChecked = true;

        let totalPrice = 0;

        let totalNum = 0;

        cart.forEach((item) => {
            if (item.checked) {
                totalPrice += item.num * item.goods_price
                totalNum += item.num
            } else {
                allChecked = false
            }
        })

        allChecked = cart.length != 0 ? allChecked : false

        this.setData({
            cart,
            allChecked,
            totalPrice,
            totalNum
        })
        wx.setStorageSync("cart", cart);
    },



    // 全选   反选
    handleAllChange() {
        let { cart, allChecked } = this.data
        allChecked = !allChecked
        console.log(allChecked);
        cart.forEach((item) => {
            item.checked = allChecked
        })

        this.setCart(cart)

    },



    // 商品数量的编辑
    async handleItemNumEdit(e) {
        const { operation, id } = e.currentTarget.dataset;

        let { cart } = this.data;

        const index = cart.findIndex((item) => item.goods_id === id);

        if (cart[index].num === 1 && operation === -1) {
            const res = await showModal({content: "您是否要删除 ?"});
            if(res.confirm){
                cart.splice(index, 1)
                this.setCart(cart)
            }
        } else {
            cart[index].num += operation

            this.setCart(cart)
        }

    },



    // 点击结算按钮
    async handlePay(){
        const { address, totalNum } = this.data
        if(!address.userName){
           await showToast({title: '您还没有选择收货地址'});
           return;   
        };
        if(totalNum === 0){
           await showToast({title: '您还没有选择商品'});
           return;  
        };

        wx.navigateTo({
            url: '/pages/pay/index',
        });
    }


})